<?php
class My_EventWeiboController extends MM_Controller_My_Action
{	
	/**
	 * var
	 */
	private $ewbid = null;
	
	/**
	 * @var MM_Service_Admin_EventWeibo
	 */
	private $serviceEWB = null;
	
	/**
	 * 初始化
	 */
	public function init()
	{
		parent::init();
		$this->serviceEWB = new MM_Service_Admin_EventWeibo();
	}
	
	/**
	 * index
	 */
	public function indexAction()
	{
		$this->helper->setPageView($this->pageview, WEB_TITLE, "eventWeiboList", null);
	}
	
	/**
	 * page
	 */
	public function pageAction()
	{
		if ( !empty($this->httpdata['act']) && isset($this->httpdata['act']) ) {
			$dt = new Mediad2_Utility_Datatable($this->serviceEWB);
			if(!isset($this->httpdata['ovar'])) {
				$this->httpdata['ovar'] = "addtime";
			}
			$dt->init($this->httpdata['act'], array("name"));
			$dt->create();
			$dt->response();
		}
	}
	
	/**
	 * list event weibo
	 */
	public function listAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$where = "eid = $this->eid";
			$order = "ewbid asc";
			$this->result = $this->serviceEWB->fetchAll(null,null,$where,$order,true);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * create event weibo
	 */
	public function createAction()
	{
		if ( ($this->uid<1) || ($this->eid<1)
    			|| !isset($this->httpdata['name']) || empty($this->httpdata['name'])
				|| !isset($this->httpdata['wbtype']) || empty($this->httpdata['wbtype'])
				|| !isset($this->httpdata['wbuid']) || empty($this->httpdata['wbuid'])
				|| !isset($this->httpdata['url']) || empty($this->httpdata['url'])
				|| !isset($this->httpdata['name']) || empty($this->httpdata['name']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/name');
    	} else {
    		//封装model
    		$model = new MM_Model_EventWeibo();
    		$model->setEid($this->eid);
    		$model->setName($this->httpdata['name']);
    		$model->setWbtype($this->httpdata['wbtype']);
    		$model->setWbuid($this->httpdate['wbuid']);
    		$model->setUrl($this->httpdata['url']);
    		$model->setAddtime(time());
    		$model->setNum(isset($this->httpdata['num'])?$this->httpdata['num']:0);
    		if (isset($this->httpdata['location'])) $model->setLocation($this->httpdata['location']);
    		if (isset($this->httpdata['avatar'])) $model->setAvatar($this->httpdata['avatar']);
    	
    		$this->result = $this->serviceEWB->insert($model);
    	}
    	echo $this->encodeResult($this->result);
	}
	
	/**
	 * update event weibo
	 */
	public function updateAction()
	{
		if ( ($this->uid<1)
				|| !isset($this->httpdata['ewbid']) || empty($this->httpdata['ewbid']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/ewbid');
		} else {
			$ewbid = $this->httpdata['ewbid'];
			$this->result = $this->serviceEWB->updateWeibo($ewbid, $this->httpdata);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * delete event weibo
	 */
	public function delAction()
	{
		if ( ($this->uid<1)
				|| !isset($this->httpdata['ewbid']) || empty($this->httpdata['ewbid']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/ewbid');
		} else {
			$ewbid = $this->httpdata['ewbid'];
			$this->result = $this->serviceEWB->delete($ewbid);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get event weibo type from dict
	 */
	public function getweibotypeAction()
	{
		if ( ($this->uid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid');
		} else {
			$this->result = $this->serviceEWB->getWeiboType();
		}
		echo $this->encodeResult($this->result);
	}
	
}